package com.gopay.bis.web.controller.ebank;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.BocConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.boc.ebank.BocB2bMessage;

/**
 * @BocB2bServerRes.java
 * @author wanggang
 * @2013-11-7 下午2:52:53 www.gopay.com.cn Inc.All rights reserved.
 */
@Controller
@Scope("prototype")
public class BocB2bServerRes extends BankResBase {

	private static Log logger = LogFactory.getLog(BocB2bServerRes.class);

	@RequestMapping(value = "/BocB2BServerRes")
	protected ModelAndView execute(HttpServletRequest request,HttpServletResponse response) {
		logger.error("--------------- BocB2BServerRes bankback ---------------");
		return super.execute(request, response);
	}

	@Override
	protected BocB2bMessage buildMessage(HttpServletRequest request) {
		BocB2bMessage message = new BocB2bMessage();
		message.setBank(BankConstants.BANK_LOGO_BOC);
		message.setTranSeq(request.getParameter("tranSeq"));
		message.setBankOrderId(request.getParameter("orderNo"));
		// 交易流水，银行网关生成的交易流水
		message.setTranCode(request.getParameter("tranCode"));
		// 网关商户号
		message.setBocNo(request.getParameter("bocNo"));
		// 商户订单号，平台对银行的订单号
		message.setOrderId(request.getParameter("orderNo"));
		// 订单币种--目前中行只支持001人民币
		message.setCurCode(request.getParameter("curCode"));
		// 交易费用
		message.setFeeAmount(request.getParameter("feeAmount"));
		// 交易金额
		message.setTranTime(request.getParameter("tranTime"));
		// 交易金额
		message.setAmount(new BigDecimal(request.getParameter("tranAmount")));
		// 交易状态：只有终态:成功/失败
		message.setTranStatus(request.getParameter("tranStatus"));
		// 加密串
		message.setSignMessage(request.getParameter("signData"));
		//银行业务产品01企业网银支付
		message.setBankBusProd(BisBankBusinessProd._01);
		return message;
	}

	@Override
	protected OrderInfo buildOrderInfo(HttpServletRequest request, Result result) {
		OrderInfo orderInfo = new OrderInfo();
		String tranStatus = request.getParameter("tranStatus");
		if (BocConstants.B2B_PAY_ORDER_STATUS_SUCCESS.equals(tranStatus)) {
			orderInfo.setOrderId(request.getParameter("orderNo"));
			orderInfo.setAmt(request.getParameter("tranAmount"));
			orderInfo.setNotifyNum("1");
			orderInfo.setPayStatus("00");
			orderInfo.setRetCode("00");
			orderInfo.setRetMsg("");
		} else {
			orderInfo.setOrderId(request.getParameter("orderNo"));
			orderInfo.setAmt(request.getParameter("tranAmount"));
			orderInfo.setNotifyNum("1");
			orderInfo.setPayStatus("01");
			orderInfo.setRetCode("01");
			orderInfo.setRetMsg("");
		}
		return orderInfo;
	}

	@Override
	protected boolean checkParameters(HttpServletRequest request) {
		if (StringUtils.isEmpty(request.getParameter("bocNo"))
				|| StringUtils.isEmpty(request.getParameter("orderNo"))
				|| StringUtils.isEmpty(request.getParameter("tranStatus"))
				|| StringUtils.isEmpty(request.getParameter("signData"))) {
			return false;
		}
		return true;
	}

}
